import ddddocr
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get('http://www.lagoushop.com/index.php/Home/user/login.html')
driver.maximize_window()
driver.implicitly_wait(30)
username = WebDriverWait(driver, 10, 1).until(
            EC.presence_of_element_located((By.ID, 'username'))
        ).send_keys(str(13800138006))
password = WebDriverWait(driver, 10, 1).until(
            EC.presence_of_element_located((By.ID, 'password'))
        ).send_keys(str(123456))
# 验证码图片区域
code_img_box = WebDriverWait(driver, 10, 1).until(
            EC.presence_of_element_located((By.ID, 'verify_code_img'))
        )
code_img_box.screenshot('verify_code.img')

# 创建ddddocr对象
ocr = ddddocr.DdddOcr()

# 读取验证码图片的bytes数据
with open('verify_code.img', 'rb') as f:
    img_bytes = f.read()

# 使用ddddocr进行识别
result = ocr.classification(img_bytes)
# 验证码输入框
code_box = WebDriverWait(driver, 10, 1).until(
            EC.presence_of_element_located((By.ID, 'verify_code'))
        ).send_keys(str(result))
# 点击登录按钮
WebDriverWait(driver, 10, 1).until(
            EC.presence_of_element_located((By.XPATH, '//*[@class="login_bnt"]/a'))
        ).click()

time.sleep(2)

driver.quit()
